package flysearch.core;

import java.io.File;
import java.util.Date;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;

public class Searcher {
    public var dataPath : String;
    public var resultCount : Number;
    
    //private properties
    var resultList : File[];

    public function search(str: String): File[]{
        var data = new File(dataPath);
        var fsDir = FSDirectory.getDirectory(data, false);
        var searcher = new IndexSearcher(fsDir);
        var query = new QueryParser("content", new StandardAnalyzer()).parse(str);
        var start = new Date().getTime();
        //println("Start Time : {start}}");

        var hits = searcher.search(query, null, resultCount).scoreDocs;

        var end = new Date().getTime();
        //println("Start Time : {end}}");
        resultList = [];

        for(hit in hits){
            var doc = searcher.doc(hit.doc);
            insert new File(doc.get("path")) into resultList;
        }

        return resultList;
    }
}
